package cn.itcast.bos.web.action.system;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.MenuService;
import cn.itcast.bos.web.action.common.BaseAction;

/**
 * 菜单管理的相关功能
 * 
 * @author 艾欣
 *
 */
@ParentPackage("json-default")
@Controller
@Namespace("/")
@Scope("prototype")
public class MenuAction extends BaseAction<Menu> {
	// 注入菜单的业务层
	@Autowired
	private MenuService ms;

	@Action(value = "menu_list", results = { @Result(name = "success", type = "json") })
	/**
	 * 查询所有菜单
	 * @return
	 */
	public String list() {
		// 调用业务层查询所有
		List<Menu> list = ms.findAll();
		// 压入值栈
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}

	/**
	 * 添加菜单的功能
	 */
	@Action(value = "menu_save", results = { @Result(name = "success", type = "redirect", location = "pages/system/menu.html") })
	public String save() {
		// 调用业务层保存数据
		ms.save(model);
		return SUCCESS;
	}
	/**
	 * 动态显示菜单
	 */
	@Action(value="menu_showmenu",results={@Result(name="success",type="json")})
	public String showMenu(){
		Subject subject = SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal();
		//通过用户查找相应的菜单
	List<Menu> list= ms.findByUser(user);
	//压入值栈
	ActionContext.getContext().getValueStack().push(list);
	return SUCCESS;
	}
}
